package com.bjc.concurrent;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * @program: aqs_study
 * @description:  糟糕写法（不要学）
 * @author: bjc
 * @create: 2021-03-22 21:20
 */
public class VolatileSerialTest {

    static volatile int x = 0,y = 0;

    public static void main(String[] args) throws InterruptedException {
        Set<Object> set = new HashSet<>();
        HashMap<String, Integer> map = new HashMap<>(16);

        for (int i = 0; i < 1000000; i++) {
            x=0;
            y=0;
            map.clear();
            Thread one = new Thread(()->{
                int a = y;
                x=1;
                map.put("a",a);
            });

            Thread other = new Thread(()->{
                int b = x;
                y = 1;
                map.put("b",b);
            });
            one.start();
            other.start();
            one.join();
            other.join();

            set.add("a="+map.get("a")+","+"b="+map.get("b"));
            System.out.println(set);
        }
    }
}
